#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
/* tree */
struct TreeNode {
    int index;
    int left;
    int right;
} ;

struct TreeNode Tree[10];

/* queue */
typedef struct Node* PtrToNode;

typedef struct TreeNode* ElementType;

struct Node {
    ElementType Data;
    PtrToNode Next;
};

typedef PtrToNode Position;

typedef struct QNode* PtrToQNode;

struct QNode {
    Position Front, Rear;
    int Maxsize;
};

typedef PtrToQNode Queue;

Queue CreateQueue ( int Maxsize ) {
    Queue Q;
    Q = (Queue)malloc(sizeof(struct QNode));
    Q->Front = NULL;
    Q->Rear = NULL;
    Q->Maxsize = Maxsize;
    return Q;
}

bool IsEmpty ( Queue Q ) {
    return ( Q->Front == NULL );
}

bool IsFull ( Queue Q ) {
    int cnt = 0;
    Position PtrQ = Q->Front;
    while( PtrQ ) {
        cnt++;
        PtrQ = PtrQ->Next;
    }
    return ( cnt == Q->Maxsize );
}

bool AddQ ( Queue Q, ElementType X ) {
    if (IsFull(Q)) {
        printf("Full\n");
        return false;
    }
    if (IsEmpty(Q)) {
        Q->Front = (Position)malloc(sizeof(struct Node));
        Q->Front->Data = X;
        Q->Rear = Q->Front;
    } else {
        Position tmp = (Position)malloc(sizeof(struct Node));
        tmp->Data = X;
        tmp->Next = NULL;
        Q->Rear->Next = tmp;
        Q->Rear = tmp;
    }
    return true;
}

ElementType DeleteQ ( Queue Q ) {
    Position FrontCell;
    ElementType FrontElem;

    if (IsEmpty(Q)) {
        printf("Empty\n");
        exit(-6);
    }

    FrontCell = Q->Front;
    if ( Q->Front == Q->Rear ) Q->Front = Q->Rear = NULL;
    else Q->Front = Q->Front->Next;
    FrontElem = FrontCell->Data;

    free(FrontCell);
    return FrontElem;
}

void find_leave_pre ( int root ) {
    if ( ( Tree[root].left == -1 ) && ( Tree[root].right == -1 ) ) printf("%d\n", root);
    else if ( ( Tree[root].left != -1 ) && ( Tree[root].right == -1 ) ) find_leave_pre(Tree[root].left);
    else if ( ( Tree[root].left == -1 ) && ( Tree[root].right != -1 ) ) find_leave_pre(Tree[root].right);
    else if ( ( Tree[root].left != -1 ) && ( Tree[root].right != -1 ) ) {
        find_leave_pre(Tree[root].left);
        find_leave_pre(Tree[root].right);
    }
}

void find_leave ( int root, int N ) {
    int output = 0;
    ElementType tmp;
    Queue Q = CreateQueue(16384);
    AddQ(Q,&(Tree[root]));
    while ( !IsEmpty(Q) ) {
        tmp = DeleteQ(Q);
        if ( ( tmp->left == -1 ) && ( tmp->right == -1 ) ) {
            if ( output == 0 ) {
                printf("%d", tmp->index);
                output = 1;
            } else printf(" %d", tmp->index);
        } if ( tmp->left != -1 ) AddQ(Q, &Tree[tmp->left]);
        if ( tmp->right != -1 ) AddQ(Q, &Tree[tmp->right]);
    }
    printf("\n");
}

int main () {
    int N, i, root;
    char left, right;
    scanf("%d", &N);
    int* check = (int*)malloc(sizeof(int)*N);
    for ( i = 0; i < N; i++ ) check[i] = 0;
    /* build tree */
    for ( i = 0; i < N; i++ ) {
        Tree[i].index = i;
        while ( getchar() != '\n' );
        scanf("%c %c", &left, &right);
        if ( isdigit(left) ) {
            Tree[i].left = left - '0';
            check[Tree[i].left] = 1;
        } else Tree[i].left = -1;
        if ( isdigit(right) ) {
            Tree[i].right = right - '0';
            check[Tree[i].right] = 1;
        } else Tree[i].right = -1;
    }
    /* search */
    for ( i = 0; i < N; i++ ) {
        if ( check[i] == 0 ) {
            root = i;
            break;
        }
    }
    find_leave( root, N );
    free(check);
    return 0;
}